package com.woniuxy.member.app.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

public class AliyunOss {

        private String endpoint="oss-cn-chengdu.aliyuncs.com";

        private String bucketUrl="oss-cn-chengdu.aliyuncs.com";

        private String accessKeyId = "LTAI5tJ5tKV1FhN3zjrauUTF";

        private String accessKeySecret ="nYdJMwugnNpmPPdTguNYsVkRs8VIWv";

        private String bucketName="woniu95";
     
        public String ossUpload(MultipartFile file, String fileKey) throws IOException {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            PutObjectResult res = ossClient.putObject(bucketName, fileKey, new ByteArrayInputStream(file. getBytes()));
            ossClient.shutdown();
            if (res != null) {
                return "https://" + bucketName + "."+ bucketUrl + "/"+fileKey;
            }
            return null;
        }
     
        public String ossUploadByteStream(String fileKey, byte[] bytes)
        {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileKey, new ByteArrayInputStream(bytes));
            PutObjectResult res = ossClient.putObject(putObjectRequest);
            ossClient.shutdown();
            if (res != null) {
                return "https://" + bucketName + "."+ bucketUrl + "/"+fileKey;
            }
            return null;
        }
     
     
        public Map<String, String> getPolicy(String dir) throws UnsupportedEncodingException {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
     
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
     
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);
     
            Map<String, String> respMap = new LinkedHashMap<String, String>();
            respMap.put("accessid", accessKeyId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("fileName", UUID.randomUUID().toString());
            respMap.put("host", bucketUrl);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            respMap.put("callback", "");
     
            ossClient.shutdown();
            return respMap;
        }
     
        public String getBucketUrl() {
            return bucketUrl;
        }

    }


